//
//  WMH_NewsDetailView.m
//  iphone.mobileoa
//
//  Created by 李林 on 13-10-24.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//  新闻详情

#import "WMH_NewsDetail_ViewController.h"
#import "EpointAppDelegate.h"

#import "UIViewController+MJPopupViewController.h"

#define getNewsDetailTaskId 1
@interface WMH_NewsDetail_ViewController ()

@end

@implementation WMH_NewsDetail_ViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        _appDelegate = (EpointAppDelegate *)[UIApplication sharedApplication].delegate;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //获取传入的参数
    InfoID      = [self.passParam objectForKey:@"InfoID"];
    CateNum     = [self.passParam objectForKey:@"CateNum"];
    IsNeedUrl   = [self.passParam objectForKey:@"IsNeedUrl"];
    
    
    pagetype = [self.passParam objectForKey:@""];
    
    //获取详情
    [self getNewsDetailData];
    
}

-(IBAction)clickMenu2{
    NSString *title = [NSString stringWithFormat:@"//分享:%@",infoTitle];
//    [self toSharedAction:title withURL:shareURL withImage:nil];
}

-(IBAction)clickMenu1{
    
}

//-(IBAction)toSharedAction:(NSString *) title  withURL:(NSString*)url withImage:(NSString *)imgpath{
//    id<ISSCAttachment> imageAttach= nil;
//    NSString * shareMsg=nil;
//    SSPublishContentMediaType mediaType = SSPublishContentMediaTypeText;
//    if (imgpath==nil) {
//        shareMsg=[NSString stringWithFormat:@"%@ %@",title,url];
//        mediaType = SSPublishContentMediaTypeText;
//    }else{
//        NSLog(@"imgPath=%@",imgpath);
//        shareMsg=[NSString stringWithFormat:@"%@",title];
//        imageAttach = [ShareSDK imageWithPath:imgpath];
//        mediaType = SSPublishContentMediaTypeImage;
//    }
//
//    //构造分享内容
//    id<ISSContent> publishContent=[ShareSDK content:shareMsg
//                                     defaultContent:@"默认分享内容,没内容时显示"
//                                              image:imageAttach title:@"分享"
//                                                url:@"" description:@"分享" mediaType:mediaType];
//    //定义菜单分享列表
//    NSArray *shareList = [ShareSDK getShareListWithType:  ShareTypeSinaWeibo, ShareTypeTencentWeibo,ShareTypeWeixiSession,ShareTypeWeixiTimeline,ShareTypeQQSpace,ShareType163Weibo,ShareTypeRenren ,ShareTypeSMS,ShareTypeMail,nil];
//    //创建分享选项
//    id<ISSShareOptions> shareOptions = [ShareSDK defaultShareOptionsWithTitle:@"分享" oneKeyShareList:shareList qqButtonHidden:YES wxSessionButtonHidden:YES wxTimelineButtonHidden:YES showKeyboardOnAppear:NO shareViewDelegate:_appDelegate.viewDelegate friendsViewDelegate:_appDelegate.viewDelegate picViewerViewDelegate:nil];
//
//    id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES
//                                                         allowCallback:YES
//                                                         authViewStyle:SSAuthViewStyleFullScreenPopup
//                                                          viewDelegate:nil
//                                               authManagerViewDelegate:_appDelegate.viewDelegate];
//
//    [ShareSDK showShareActionSheet:nil shareList:shareList
//                           content:publishContent statusBarTips:YES authOptions :authOptions shareOptions:shareOptions
//                            result:^(ShareType type, SSPublishContentState state, id<ISSStatusInfo> statusInfo,id<ICMErrorInfo>error,BOOL end){
//                                if (state == SSPublishContentStateSuccess)
//                                {
//                                    NSLog(@"分享成功");
//                                }
//
//                                else if (state == SSPublishContentStateFail) {
//                                    NSLog(@"分享失败,错误码:%d,错误描述:%@", [error errorCode],[error errorDescription]);
//                                } }];
//
//
//}

#pragma mark 获取新闻详情
- (void)getNewsDetailData
{
    [self showMBPHUD];
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:InfoID    forKey:@"InfoID"];
    [params setObject:CateNum   forKey:@"CateNum"];
    [params setObject:IsNeedUrl forKey:@"IsNeedUrl"];
    [params setObject:@"0"      forKey:@"isvideo"];
    [params setObject:@"320"    forKey:@"imgwidth"];
    [self startTask:@"Task_GetInfoDetail" withParams:params withTaskId:getNewsDetailTaskId];
}

#pragma mark 刷新UI
-(void)refreshUIWithThreadGuid:(int)taskId andData:(NSObject *)obj
{
    [self hideMBPHUD];//关闭滚动条
    //    NSString *networkstatus = [userDefaults objectForKey:NetWorkStatus];
    //    if([networkstatus isEqualToString:@"0"]){
    //        [self toastMessage:networkError];//网络异常
    //    }
    //
    if (taskId==getNewsDetailTaskId) {
        [self dealNewsDetailData:obj];
    }
}

#pragma mark 处事新闻详情数据
-(void)dealNewsDetailData:(NSObject *)obj{
    
    
    //处理异常信息
    NSMutableArray *arr = (NSMutableArray*)obj;
    if (![[arr objectAtIndex:0]isEqualToString:@"1"]) {
        [self toastMessage:[arr objectAtIndex:1]];
        return;
    }
    
    
    
    //验证返回串
    // if ([self validateXML:result]) {//校验数据
    NSString *result = [arr objectAtIndex:2];
    
    
    NSString *newscontent_fontsize = [DBFrameService getConfigValue:WMH_NewsContent_FontSize];
    
    //注意：css拼接中使用%%，进行百分号的格式化处理
    NSString * mycss =[NSString stringWithFormat:@"<style>p{margin:0px;padding:0px;}#news_outline {font-family:System;color: #5a5a5a; font-size:14px;text-align:left;padding-bottom:16px;padding-top:4px;}.ParagraphIndent img,p img{width:100%%;max-width:100%%;display:block;margin-bottom: 10px;}.ParagraphIndent,p,P{-webkit-box-sizing:border-box;box-sizing:border-box; width:100%%; padding:0 10px; letter-spacing:0.1px;color:#121212;text-indent:2em;font-size:%@px;line-height:150%%; overflow: hidden;word-wrap:break-word; word-break: bre-all;height:auto;zoom:1;text-align:justify;text-justify:inter-ideograph;padding-left:2px;font-family:System;}#news_title {font-weight:bold;text-align:left;-webkit-box-sizing:border-box;box-sizing:border-box; width:100%%;letter-spacing:0.1px;color:#121212;font-size:20px;line-height:120%%; overflow: hidden;word-wrap:break-word; word-break: bre-all;height:auto;zoom:1;text-align:justify;text-justify:inter-ideograph;font-family:System;padding-right:6px};</style>",newscontent_fontsize];
    
    result =[result stringByReplacingOccurrencesOfString:@"<![CDATA[" withString:@""];
    result= [result stringByReplacingOccurrencesOfString:@"]]>" withString:@""];
    
    infoTitle           = [EpointStringUtil getXMLFormatedAttribute :result :@"title"];
    NSString *infoDate  = [EpointStringUtil getXMLFormatedAttribute :result :@"infodate"];
    shareURL            = [EpointStringUtil getXMLFormatedAttribute :result :@"weburl"];//解析链接内容
    
    //处理附件
    if(![[EpointStringUtil getXMLFormatedAttribute:result :@"attachfiles"]isEqualToString:@""]){
        NSString *attachXML = [EpointStringUtil getXMLFormatedAttribute:result :@"attachfiles"];
        NSLog(@"有附件：%@",attachXML);
    }
    
    result=[EpointStringUtil getXMLFormatedAttribute:result :@"infocontent"];
    
    
    //对图片URL增加网址
    if([result rangeOfString:@"src=\""].location !=NSNotFound){
        
        result=[result stringByReplacingOccurrencesOfString:@"<img " withString:@"<img  onclick = \"document.location=this.src\" "];
        result=[result stringByReplacingOccurrencesOfString:@"<IMG " withString:@"<IMG  onclick = \"document.location=this.src\" "];
    }
    
    NSString *disContent = [NSString stringWithFormat:@"%@<div id='news_title'>%@</div><div id='news_outline'>%@ %@</div>%@<br />",mycss,infoTitle,@"",[self dealDate:infoDate],result];
    
    [self.mWebView loadHTMLString:disContent baseURL:nil];
    self.mWebView.delegate = self;
    
   
}

#pragma 处理日期类型
-(NSString *)dealDate:(NSString *)_infoDate{
    if([_infoDate rangeOfString:@"/"].location!=NSNotFound || [_infoDate rangeOfString:@"-"].location!=NSNotFound){
        NSArray *str = [_infoDate componentsSeparatedByString:@" "];
        NSString *temp = [NSString stringWithFormat:@"%@: %@",@"发布时间",[str objectAtIndex:0]];
        return  temp;
    }else{
        return [NSString stringWithFormat:@"%@: %@",@"发布时间",_infoDate];
    }
}

#pragma mark webview代理方法
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    //显示图片预览
    NSString* rurl=[[request URL] absoluteString];
    if ([rurl hasPrefix:@"http://"]) {
        
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        
        [params setObject:rurl forKey:@"src"];
        [params setObject:infoTitle forKey:@"infoTitle"];
        
        WMH_NewsPic_ViewController *view = [[WMH_NewsPic_ViewController alloc]initWithParams:params];
        view.parentView = self;
        view.dele = self;
        [self presentViewController:view animated:YES completion:nil];
        
        return false;
    }
    
    return true;
}

#pragma mark 关闭大图
-(void)back{
    [self dismissPopupViewControllerWithanimationType:MJPopupViewAnimationFade];
}

@end
